/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cgi.ndk.wf.bp;

import com.cgi.ndk.wf.soa.SoaClient;
import com.cgi.ndk.wf.generated.endToEndProcessOnly.EndToEndProcessOnlyRequest;
import com.cgi.ndk.wf.generated.endToEndProcessOnly.EndToEndProcessOnlyResponse;
import com.cgi.ndk.wf.generated.endToEndTest.EndToEndTestRequest;
import com.cgi.ndk.wf.generated.endToEndTest.EndToEndTestResponse;
import com.cgi.ndk.wf.generated.humanTaskService.Claim;
import com.cgi.ndk.wf.generated.humanTaskService.ClaimResponse;
import com.cgi.ndk.wf.generated.humanTaskService.Complete;
import com.cgi.ndk.wf.generated.humanTaskService.CompleteResponse;
import com.cgi.ndk.wf.generated.humanTaskService.LoadTask;
import com.cgi.ndk.wf.generated.humanTaskService.LoadTaskResponse;
import com.cgi.ndk.wf.generated.humanTaskService.Release;
import com.cgi.ndk.wf.generated.humanTaskService.ReleaseResponse;
import com.cgi.ndk.wf.generated.humanTaskService.SimpleQuery;
import com.cgi.ndk.wf.generated.humanTaskService.SimpleQueryResponse;
import com.cgi.ndk.wf.generated.humanTaskService.Start;
import com.cgi.ndk.wf.generated.humanTaskService.StartResponse;
import com.cgi.ndk.wf.generated.humanTaskService.TSimpleQueryInput;
import com.cgi.ndk.wf.generated.instanceManagementService.GetInstanceInfoIn;
import com.cgi.ndk.wf.generated.instanceManagementService.GetPaginatedInstanceListInputType;
import com.cgi.ndk.wf.generated.instanceManagementService.InstanceInfoType;
import com.cgi.ndk.wf.generated.instanceManagementService.ObjectFactory;
import com.cgi.ndk.wf.generated.instanceManagementService.PaginatedInstanceList;
import javax.xml.bind.JAXBElement;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

/**
 *
 * @author kovalcikm
 */
@Service
public class BPService extends WebServiceGatewaySupport {

    private static final String INSTANCE_MNG_URI = "http://10.28.6.9:8280/services/InstanceManagementService/";
    private static final String INSTANCE_MNG_CONTEXT = "com.cgi.ndk.wf.generated.instanceManagementService";

    private static final String HUMAN_TASK_URI = "http://10.28.6.9:8280/services/HumanTaskClientAPIAdmin/";
    private static final String HUMAN_TASK_CONTEXT = "com.cgi.ndk.wf.generated.humanTaskService";

    private static final String END_TO_END_TEST_URI = "http://10.28.6.9:8280/services/EndToEndTest/";
    private static final String END_TO_END_PROCESS_URI = "http://10.28.6.9:8280/services/EndToEndProcessOnly/";

    private static final String END_TO_END_PROCESS_CONTEXT = "com.cgi.ndk.wf.generated.endToEndProcessOnly";
    private static final String END_TO_END_TEST = "com.cgi.ndk.wf.generated.endToEndTest";

    private static final int MAX_LIMIT = 1000000;

    public PaginatedInstanceList getInstances(Integer page, Integer limit) {

        GetPaginatedInstanceListInputType inputType = new GetPaginatedInstanceListInputType();
        inputType.setFilter("");
        inputType.setOrder("");

        if (limit == null) {
            inputType.setLimit(MAX_LIMIT);
        } else {
            inputType.setLimit(limit);
        }

        if (page != null) {
            inputType.setPage(page);
        }
        JAXBElement<GetPaginatedInstanceListInputType> obj = new ObjectFactory().createGetPaginatedInstanceListInput(inputType);

        PaginatedInstanceList response = (PaginatedInstanceList) new SoaClient().geResponse(obj, INSTANCE_MNG_URI, "urn:getPaginatedInstanceList", INSTANCE_MNG_CONTEXT);
        return response;
    }

    public InstanceInfoType getAutomaticTask(long id) {
        GetInstanceInfoIn isntanceInfo = new GetInstanceInfoIn();
        isntanceInfo.setIid(id);

        JAXBElement response = (JAXBElement) new SoaClient().geResponse(isntanceInfo, INSTANCE_MNG_URI, "urn:getInstanceInfo", INSTANCE_MNG_CONTEXT);
        InstanceInfoType info = (InstanceInfoType) response.getValue();
        return info;
    }

    public SimpleQueryResponse getHumanTasks(Integer page, Integer limit, XMLGregorianCalendar date) {
        SimpleQuery simpleQuery = new SimpleQuery();
        TSimpleQueryInput queryInput = new TSimpleQueryInput();
        if (page == null) {
            queryInput.setPageNumber(page);
        }
        if (limit == null) {
            queryInput.setPageSize(MAX_LIMIT);
        }
        if (date == null) {
            queryInput.setCreatedDate(date);
        }
        simpleQuery.setSimpleQueryInput(queryInput);

        SimpleQueryResponse response = (SimpleQueryResponse) new SoaClient().geResponse(simpleQuery, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/simpleQuery", HUMAN_TASK_CONTEXT);
        return response;
    }

    public StartResponse reserve(String id) { //Claim in BP
        Claim claim = new Claim();
        claim.setIdentifier(id);
        ClaimResponse response = (ClaimResponse) new SoaClient().geResponse(claim, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/claim", HUMAN_TASK_CONTEXT);
        //todo check response

        //and immediately change state to in progress
        Start start = new Start();
        start.setIdentifier(id);
        StartResponse startedResponse = (StartResponse) new SoaClient().geResponse(start, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/start", HUMAN_TASK_CONTEXT);

        return startedResponse;
    }

    public LoadTaskResponse load(String id) {
        LoadTask load = new LoadTask();
        load.setIdentifier(id);

        LoadTaskResponse response = (LoadTaskResponse) new SoaClient().geResponse(load, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/loadTask", HUMAN_TASK_CONTEXT);
        return response;
    }

    public CompleteResponse complete(String id) {
        Complete complete = new Complete();
        complete.setIdentifier(id);
        complete.setTaskData("<sch:SimpleTaskResponse xmlns:sch=\"http://www.example.com/SimpleTask/schema\"></sch:SimpleTaskResponse>");

        CompleteResponse response = (CompleteResponse) new SoaClient().geResponse(complete, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/complete", HUMAN_TASK_CONTEXT);
        return response;
    }

    public EndToEndProcessOnlyResponse endToEndProcessOnly(String id) {
        EndToEndProcessOnlyRequest request = new EndToEndProcessOnlyRequest();
        request.setCorrelationId(id);

        EndToEndProcessOnlyResponse response = (EndToEndProcessOnlyResponse) new SoaClient().geResponse(request, END_TO_END_PROCESS_URI, "http://wso2.org/bps/sample/process", END_TO_END_PROCESS_CONTEXT);
        return response;
    }

    public EndToEndTestResponse endToEndTest(String id) {
        EndToEndTestRequest request = new EndToEndTestRequest();
        request.setCorrelationId(id);

        EndToEndTestResponse response = (EndToEndTestResponse) new SoaClient().geResponse(request, END_TO_END_TEST_URI, "http://wso2.org/bps/sample/process", END_TO_END_TEST);
        return response;
    }

    public ReleaseResponse release(String id) {
        Release releaseRequest = new Release();
        releaseRequest.setIdentifier(id);
        ReleaseResponse response = (ReleaseResponse) new SoaClient().geResponse(releaseRequest, HUMAN_TASK_URI, "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/release", HUMAN_TASK_CONTEXT);
        return response;
    }
}
